/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gpaltan.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author srkrishnan
 */
public class DBProduct
{

    static ArrayList<String> columnlist = new ArrayList<String>();
    static ArrayList<String[]> rowdata = new ArrayList<String[]>();

    public static ArrayList<String> getColumns(DBInfo dbinfo)
    {
        columnlist = new ArrayList<String>();
        String query = "SHOW COLUMNS FROM ps_product_lang";
        addColumnnames(dbinfo, query);

        query = "SHOW COLUMNS FROM ps_product";
        addColumnnames(dbinfo, query);

        return columnlist;
    }

    public static ArrayList<String> getColumns(DBInfo dbinfo, String tablename)
    {
        columnlist = new ArrayList<String>();
        String query = "SHOW COLUMNS FROM " + tablename;
        addColumnnames(dbinfo, query);

        return columnlist;
    }

    private static void addColumnnames(DBInfo dbinfo, String query)
    {
        Connection conn = dbinfo.getDbmanager().getConnection();
        ResultSet rs = DBUtil.execute(conn, query);
        try
        {
            while (rs.next())
            {
                String columnname = rs.getString("Field");
                columnlist.add(columnname);
            }
        } catch (SQLException ex)
        {
            Logger.getLogger(DBProduct.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static ArrayList<String[]> getRecords(DBInfo dbinfo,Boolean active)
    {
        rowdata = new ArrayList<String[]>();
//        String query = "select * from ps_product_lang";
//        addRows(dbinfo, query);
//
//        query = "select * from ps_product";
//        addRows(dbinfo, query);

        String query = "select * from ps_product_lang pl "
                + "left join ps_product p on pl.id_product=p.id_product "
                + "where p.active = "+active;
        addRows(dbinfo, query);

        return rowdata;
    }

    private static void addRows(DBInfo dbinfo, String query)
    {
        Connection conn = dbinfo.getDbmanager().getConnection();
        ResultSet rs = DBUtil.execute(conn, query);
        try
        {
            String[] row = null;

            while (rs.next())
            {
                row = new String[columnlist.size() + 1];
                ResultSetMetaData rsmetadata = rs.getMetaData();
                for (int i = 0; i < rsmetadata.getColumnCount(); i++)
                {
                    row[i] = "" + rs.getObject(i + 1);
                }
                rowdata.add(row);
            }

        } catch (SQLException ex)
        {
            Logger.getLogger(DBProduct.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
